package io.github.tuuzed.hydrogen;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;

import io.github.tuuzed.hydrogen.util.Logger;


public class Model implements Serializable {
    private static final String TAG = "Hydrogen.Model";
    private int id;

    public int getId() {
        return id;
    }

    void setId(int id) {
        this.id = id;
    }

    /**
     * 保存对象
     */
    public void save() {
        ContentValues values = getContentValues();
        if (values == null) {
            Logger.d(TAG, "values is null");
            return;
        }
        Logger.d(TAG, values.toString());
        String tableName = Hydrogen.getInstance().getTableName(this.getClass());
        SQLiteDatabase db = Hydrogen.getInstance().getWritableDatabase();
        int id = this.id;
        if (id == 0) {
            db.insert(tableName, null, values);
        } else {
            db.update(tableName, values, "_id = ?", new String[]{String.valueOf(id)});
        }
        db.close();
    }

    /**
     * 删除
     */
    public int delete() {
        return new Delete(this.getClass())
                .where("_id = ?", new String[]{this.id + ""})
                .exec();
    }

    private ContentValues getContentValues() {
        Class<? extends Model> clazz = this.getClass();
        List<Hydrogen.ColumnInfo> columnInfoList = Hydrogen.getInstance().getColumnInfoList(clazz);
        ContentValues values = new ContentValues(clazz.getDeclaredAnnotations().length);
        for (Hydrogen.ColumnInfo columnInfo : columnInfoList) {
            Field field = columnInfo.getField();
            field.setAccessible(true);
            putValue(values, field.getType().getName(), columnInfo.getColumnName(), field);
        }
        return values;
    }

    private void putValue(ContentValues values, String type, String columnName, Field field) {
        Object obj = null;
        try {
            obj = field.get(this);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        if (obj == null) return;
        if (String.class.getName().equals(type)) {
            //String
            values.put(columnName, (String) obj);
        } else if (type.equals(Character.class.getName()) || "char".equals(type)) {
            // Char or char
            values.put(columnName, String.valueOf((char) obj));
        } else if (Integer.class.getName().equals(type) || "int".equals(type)) {
            // Integer or int
            values.put(columnName, (int) obj);
        } else if (Boolean.class.getName().equals(type) || "boolean".equals(type)) {
            // Boolean or boolean
            values.put(columnName, (boolean) obj);
        } else if (Byte.class.getName().equals(type) || "byte".equals(type)) {
            // Byte or byte
            values.put(columnName, (byte) obj);
        } else if (Double.class.getName().equals(type) || "double".equals(type)) {
            // Double or double
            values.put(columnName, (double) obj);
        } else if (Float.class.getName().equals(type) || "float".equals(type)) {
            // Float or float
            values.put(columnName, (float) obj);
        } else if (Short.class.getName().equals(type) || "short".equals(type)) {
            // Short or short
            values.put(columnName, (short) obj);
        } else if (Long.class.getName().equals(type) || "long".equals(type)) {
            // Long or long
            values.put(columnName, (long) obj);
        }
    }
}
